package com.shop2cn.iapi.sqdistributor.testcase;

import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.sqdistributor.parameter.request.GetConsumerManageListReq;
import com.shop2cn.iapi.sqdistributor.parameter.response.pojo.ConsumerInfoVo;
import com.shop2cn.iapi.sqdistributor.service.GetConsumerManageListCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.OrderCallService;
import com.ymttest.business.service.shenqi.SqDistributorService;
import com.ymttest.business.service.shenqi.bean.ShenqiUserSet;
import com.ymttest.database.model.sqdistributor.DistributorConsumerRelation;
import com.ymttest.database.sqlwapper.sqdistributor.DistributorConsumerRelationWrapper;
import com.ymttest.utils.DataUtil;

public class Ts_GetConsumerManageList {
	
	//java接口
	private static GetConsumerManageListReq getConsumerManageListReq;
	private static GetConsumerManageListCall getConsumerManageListCall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("买家管理列表");
	}

	@Before
	public void caseUp() {
		getConsumerManageListReq = new GetConsumerManageListReq();
		getConsumerManageListCall = new GetConsumerManageListCall();	
	}
	
	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getConsumerManageListCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	public void verifyDb() throws Exception {
		
		DistributorConsumerRelationWrapper distributorConsumerRelationWrapper = new DistributorConsumerRelationWrapper();
		
		Long merchantId = SqDistributorService
				.getMasterShopId(getConsumerManageListReq.getMerchantId());

		if (merchantId == 0L)
			merchantId = getConsumerManageListReq.getMerchantId();
		
		DistributorConsumerRelation actualResult = distributorConsumerRelationWrapper
				.selectByMerchantIdAndDistributorId(merchantId,
						getConsumerManageListReq.getDistributorId());
		
		List<ConsumerInfoVo> expectList = getConsumerManageListCall
				.getConsumerManageListResponse().getConsumerInfoVoList();
		
		for (int i = 0; i < expectList.size(); i++) {
			
			ConsumerInfoVo expectResult = expectList.get(i);

			Logger.verifyEquals(expectResult.getId(), actualResult.getId(),
					"验证Id");
			Logger.verifyEquals(expectResult.getUserId(),
					actualResult.getUser_id(), "验证User_id");
			Logger.verifyEquals(expectResult.getUserName(),
					actualResult.getUser_name(), "验证User_name");
			Logger.verifyEquals(expectResult.getUserPhone(),
					actualResult.getUser_phone(), "验证User_phone");
			Logger.verifyEquals(expectResult.getDistributorId(),
					actualResult.getDistributor_id(), "验证Distributor_id");
			Logger.verifyEquals(expectResult.getMerchantId(),
					actualResult.getMerchant_id(), "验证Merchant_id");
			DataUtil.verifyDateEquals(expectResult.getBindingTime(),
					actualResult.getBinding_time(), "验证Binding_time");
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetConsumerManageList_001() {
		Logger.start(true, "买家管理列表查询成功, 单个用户");
		
		try {
			
			getConsumerManageListReq.setMerchantId(500861041L);
			getConsumerManageListReq.setUserId(73992151L);
			getConsumerManageListReq.setDistributorId(500861041L);
			getConsumerManageListReq.setUserName("郑莘");
			getConsumerManageListReq.setUserPhone("13149018701");
			getConsumerManageListReq.setStartTime("2020-04-20 15:22:46");
			getConsumerManageListReq.setEndTime("2020-04-30 15:22:46");
			getConsumerManageListReq.setPageIndex(1);
			getConsumerManageListReq.setPageSize(10);
			
			getConsumerManageListCall.setData(getConsumerManageListReq);
			getConsumerManageListCall.callService();
			
			Logger.verifyEquals(true, getConsumerManageListCall.getSuccess(), "验证success");
			
			verifyDb();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetConsumerManageList_002() {
		Logger.start(true, "买家管理列表查询成功, 多个用户");
		
		try {
			
			getConsumerManageListReq.setMerchantId(500002490L);
			getConsumerManageListReq.setDistributorId(26146983L);
			getConsumerManageListReq.setPageIndex(1);
			getConsumerManageListReq.setPageSize(10);
			
			getConsumerManageListCall.setData(getConsumerManageListReq);
			getConsumerManageListCall.callService();
			
			Logger.verifyEquals(true, getConsumerManageListCall.getSuccess(), "验证success");
			
			verifyDb();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetConsumerManageList_003() {
		Logger.start(true, "买家管理列表查询成功, 没有符合条件的数据");
		
		try {
			
			getConsumerManageListReq.setMerchantId(500861041L);
			getConsumerManageListReq.setDistributorId(5008610412L);
			getConsumerManageListReq.setUserId(73992151L);
			getConsumerManageListReq.setUserName("郑莘");
			getConsumerManageListReq.setUserPhone("13149018701");
			getConsumerManageListReq.setStartTime("2020-03-20 15:22:46");
			getConsumerManageListReq.setEndTime("2020-04-01 15:22:46");
			getConsumerManageListReq.setPageIndex(1);
			getConsumerManageListReq.setPageSize(10);
			
			getConsumerManageListCall.setData(getConsumerManageListReq);
			getConsumerManageListCall.callService();
			
			Logger.verifyEquals(true, getConsumerManageListCall.getSuccess(), "验证success");
			Logger.verifyEquals(null, getConsumerManageListCall
					.getResult(), "验证ConsumerInfoVoList列表大小");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetConsumerManageList_004() {
		Logger.start(true, "买家管理列表无法查询被删除的用户");
		
		try {
			
			getConsumerManageListReq.setMerchantId(500861041L);
			getConsumerManageListReq.setUserId(7706878L);
			getConsumerManageListReq.setDistributorId(500861041L);
			getConsumerManageListReq.setPageIndex(1);
			getConsumerManageListReq.setPageSize(10);
			
			getConsumerManageListCall.setData(getConsumerManageListReq);
			getConsumerManageListCall.callService();
			
			Logger.verifyEquals(true, getConsumerManageListCall.getSuccess(), "验证success");
			Logger.verifyEquals(0, getConsumerManageListCall
					.getConsumerManageListResponse().getConsumerInfoVoList()
					.size(), "验证ConsumerInfoVoList列表大小");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetConsumerManageList_005() {
		Logger.start(true, "子店铺，买家管理列表查询成功, 多个用户");
		
		try {
			
			long merchantId = ShenqiUserSet.autotest_goodsOwnerId;
			Long userId = (long) new OrderCallService().createNewUser();
			
			List<Long> distributorList = SqDistributorService
					.buildSqDistributorRelation(merchantId, 0, 2, 0);

			SqDistributorService.setDistributorConfig(merchantId, 2, false);
			SqDistributorService.bindDistributorConsumerRelation(userId,
					merchantId, distributorList.get(1));
			
			getConsumerManageListReq.setMerchantId(ShenqiUserSet.chainStoreGoodsOwnerId);
			getConsumerManageListReq.setDistributorId(distributorList.get(1));
			getConsumerManageListReq.setPageIndex(1);
			getConsumerManageListReq.setPageSize(10);
			
			getConsumerManageListCall.setData(getConsumerManageListReq);
			getConsumerManageListCall.callService();
			
			Logger.verifyEquals(true, getConsumerManageListCall.getSuccess(), "验证success");
			
			verifyDb();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}
